
class ComicScene extends egret.DisplayObjectContainer{
	public vo:ComicSceneVO;

	private _sceneArmature:dragonBones.EgretArmatureDisplay;
	private _curAnimState:dragonBones.AnimationState;
	private _curShot:ComicShotVO;
	private _dia:Dialog;
	private _selectDia:SelectDialog;
	private dbFactory:dragonBones.EgretFactory;
	private _inited:boolean;
	private _initShotVO:ComicShotVO;

	public constructor(vo:ComicSceneVO, dia:Dialog, selectDia:SelectDialog) {
		super();
		this.vo = vo;
		this._dia = dia;
		this._selectDia = selectDia;
		this.dbFactory = dragonBones.EgretFactory.factory;
		this.dbFactory.autoSearch = true;
		this.init();
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAdded,this);
	}

	public destory():void
	{
		this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAdded,this);
		this.stage.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap,this);
	}
	private onAdded(e:egret.Event):void
	{
		this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap,this);
	}
	private onTap(e:egret.TouchEvent):void
	{
		if(this._curShot)
		{
			if(this._curShot.type == ComicShotVO.TYPE_ANIM)
			{
				if(!this._curShot.playComplete)
				{
					if(this._curShot.skipEnable)
					{
						this._sceneArmature.animation.gotoAndStopByProgress(this._curShot.name,1);
						this.dispatchEvent(new egret.Event(EVENT.SHOT_COMPLETE));
					}
					
				}
				else if(!this._curShot.autoPlay)
				{
					this.playNextShot();
				}
			}
			else if(this._curShot.type == ComicShotVO.TYPE_DIALOG)
			{
				if(this._curShot.saying)
				{
					if((!this._curShot.playComplete || !this._curShot.dialogComplete) && this._curShot.skipEnable)
					{
						this._dia.next();
						this._curShot.dialogComplete = true;
						this._sceneArmature.animation.gotoAndStopByProgress(this._curShot.name,1);
						this._curShot.playComplete = true;
						this._curShot.dialogComplete = true;
						
						this.dispatchEvent(new egret.Event(EVENT.SHOT_COMPLETE));
					}
					else
					{
						if(!this._curShot.autoPlay)
						{
							this.playNextShot();
						}
					}
				}
				else if(this._curShot.playComplete && this._curShot.dialogComplete)
				{
					if(!this._curShot.autoPlay)
					{
						this.playNextShot();
					}
				}
				
			}
		}
	}

	private async initRole():Promise<void>
	{
		await ComicResourceManager.instance.loadRoleAsset("role","shot","cloth");
		//装载角色
		this.setupRole();
		//装载衣服
		this.setupCloth();
	}
	private setupRole():void
	{
		let roleAssets = this.vo.getRoleAssets();
		let i:number;
		let len:number;
		for(i = 0, len = roleAssets.length; i < len; i++)
		{
			this.setupDB(roleAssets[i])
		}
	}
	private setupCloth():void
	{
		let clothAssets = this.vo.getClothAssets();
		let i:number;
		let len:number;
		for(i = 0, len = clothAssets.length; i < len; i++)
		{
			this.setupDB(clothAssets[i])
		}
	}
	private setupDB(dbConfig:DBConfig):void
	{
		const factory = this.dbFactory;
		ComicResourceManager.instance.loadDBConfig(factory, dbConfig);
	}

	private roleChangeCloth(sceneArmature:dragonBones.Armature):void
	{
		let i:number;
		let len:number;

		for(i = 0, len = this.vo.roles.length; i < len; i++)
		{
			let role:ComicRoleVO = this.vo.roles[i];
			let clothes = role.clothes;
			if(clothes && clothes.length > 0)
			{
				let roleSlot = sceneArmature.getSlot(role.name);
				if(roleSlot)
				{
					let roleArmature = roleSlot.childArmature;
					if(roleArmature)
					{
						let j:number;
						let jlen:number;
						for(j = 0, jlen = clothes.length; j < jlen; j++)
						{
							let cloth = clothes[j];
							
							let clothAsset = this.dbFactory.getArmatureData(cloth.armature, cloth.data);
							if(clothAsset)
							{
								this.dbFactory.changeSkin(roleArmature,clothAsset.defaultSkin);
							}
						}
					}
				}
			}
		}	
	}
	private async init():Promise<void>
	{
		await this.initRole();
		this.initScene();
		
	}
	private initScene():void
	{
		const factory = this.dbFactory;
		ComicResourceManager.instance.loadDBConfig(factory, this.vo.asset);
        this._sceneArmature = factory.buildArmatureDisplay(this.vo.name);
        var sceneArm:dragonBones.Armature = this._sceneArmature.armature;
		this.roleChangeCloth(sceneArm);

		this._sceneArmature.addEvent(dragonBones.AnimationEvent.LOOP_COMPLETE,this.onSceneAnimationComplete, this);
        this.addChild(this._sceneArmature);

		this.addEventListener(EVENT.SHOT_COMPLETE,this.onShotComplete,this)

		this._sceneArmature.addEvent(dragonBones.FrameEvent.ANIMATION_FRAME_EVENT, this.onSceneArmatureFrameEvent, this);
		
		this._dia.addEventListener(EVENT.DIALOG_OVER,this.onDialogOver,this);

		this._selectDia.addEventListener(EVENT.DIALOG_SELECT,this.onDialogSelect,this)

		this._inited = true;
		if(this._initShotVO != null)
		{
			this.playShot(this._initShotVO);
			this._initShotVO = null;
		}
	}

	private removeAllEvent():void
	{
		if(this._sceneArmature)
		{
			this._sceneArmature.removeEvent(dragonBones.AnimationEvent.LOOP_COMPLETE,this.onSceneAnimationComplete, this);
			this._sceneArmature.removeEvent(dragonBones.FrameEvent.ANIMATION_FRAME_EVENT, this.onSceneArmatureFrameEvent, this);
		}
		
		this.removeEventListener(EVENT.SHOT_COMPLETE,this.onShotComplete,this)
		
		if(this._dia)
		{
			this._dia.removeEventListener(EVENT.DIALOG_OVER,this.onDialogOver,this);
		}
		if(this._selectDia)
		{
			this._selectDia.removeEventListener(EVENT.DIALOG_SELECT,this.onDialogSelect,this)
		}	
	}
	private onDialogSelect(e:egret.Event):void
	{
		this._selectDia.hide();
		this._curShot.dialogComplete = true;
		this._curShot.saying = false;
		this._curShot.selectIndex = e.data;
		this.checkShotOver();
	}
	private onDialogOver(e:egret.Event):void
	{
		this._curShot.dialogComplete = true;
		this._curShot.saying = false;
		this.checkShotOver();
	}
	private onSceneArmatureFrameEvent(e:dragonBones.FrameEvent):void
	{
		var a:dragonBones.UserData
		if(e.data && e.data.name)
		{
			switch(e.data.name)
			{
				case "dia":
					let bone:dragonBones.Bone = e.data.bone;
					let userData:dragonBones.UserData = e.data.data;
					let diaContent:string = userData.strings[0];
					let speed:number = userData.floats[0] || 300;
					this._dia.speed = speed;
					this._dia.label = diaContent;
					this._dia.show();
					this._curShot.saying = true;
					break;
				case "select":
					bone = e.data.bone;
					userData = e.data.data;
					diaContent = userData.strings[0];
					let selectItems:string[] = diaContent.split('||');
					this._selectDia.data = selectItems;
					this._selectDia.show();
					this._curShot.saying = true;
					break;
			}
		}
	}
	private onSceneAnimationComplete(e:dragonBones.Event):void
	{
		this._curAnimState.stop();
		this.dispatchEvent(new egret.Event(EVENT.SHOT_COMPLETE));
	}
	private onShotComplete(e:egret.Event):void
	{
		console.log(this._curShot.name + "  -complete");
		this._curShot.playComplete = true;
		if(this._curShot.nextShot)
		{
			this.checkShotOver();
		}
		else
		{
			this.dispatchEvent(new egret.Event(EVENT.MOVIE_COMPLETE));
		}
	}
	public delayPlayShot(shot:ComicShotVO):void
	{
		if(this._inited)
		{
			this.playShot(shot);
		}
		else
		{
			this._initShotVO = shot;
		}
	}
	private playShot(shot:ComicShotVO):void
	{
		this._dia.hide();
		this._curShot = shot;
		this._curAnimState = this._sceneArmature.animation.play(shot.name);
	}

	private curShotOver():void
	{
		if(this._curShot.skipEnable)
		{
			this.playNextShot();
		}
	}
	private playNextShot():void
	{
		this.dispatchEvent(new egret.Event(EVENT.PLAY_NEXT_SHOT));
	}
	private checkShotOver():void
	{
		if(this._curShot)
		{
			switch(this._curShot.type)
			{
				case ComicShotVO.TYPE_ANIM:
					if(this._curShot.playComplete)
					{
						this.curShotOver();
					}
					break;
				case ComicShotVO.TYPE_DIALOG:
					if(this._curShot.playComplete && this._curShot.dialogComplete)
					{
						this.curShotOver();
					}
					break;
				case ComicShotVO.TYPE_SELECT:
					if(this._curShot.playComplete && this._curShot.dialogComplete
					   && this._curShot.selectIndex >= 0)
					{
						this.curShotOver();
					}
					break;
			}
		}
	}
	public stop():void
	{
		if(this._curAnimState)
		{
			this._curAnimState.stop();
		}
		if(this._sceneArmature)
		{
			this._sceneArmature.animation.stop();
			// this._sceneArmature.dispose(true);
			// this.removeAllEvent();
		}
		
	}
}